package com.azusa.xbjy.config;

import com.google.code.kaptcha.servlet.KaptchaServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.ServletException;

@Configuration
public class KaptchaConfiguration {

    //注册servlet
    @Bean(name = "kaptchaServlet")
    public ServletRegistrationBean servletRegistrationBean() throws ServletException {
        //注册KaptchaServlet类到servlet容器中，并指定映射路径
        ServletRegistrationBean servlet = new ServletRegistrationBean(new KaptchaServlet(), "/code.jpg");
        /*配置Kaptcha相关初始化参数*/
        //是否有边框
        servlet.addInitParameter("kaptcha.border","no");
        //字体颜色
        servlet.addInitParameter("kaptcha.textproducer.font.color","red");
        //字体大小
        servlet.addInitParameter("kaptcha.textproducer.font.size","33");
        //字体样式<!--Arial是宋体字-->
        servlet.addInitParameter("kaptcha.textproducer.font.names","Monospaced");
        //使用哪些字符生成验证码
        servlet.addInitParameter("kaptcha.textproducer.char.string","1234567890");
        //图片验证码字符个数
        servlet.addInitParameter("kaptcha.textproducer.char.length","4");
        //图片宽度
        servlet.addInitParameter("kaptcha.image.width","135");
        //图片高度
        servlet.addInitParameter("kaptcha.image.height","50");
        //干扰线的颜色
        servlet.addInitParameter("kaptcha.noise.color","black");
        //session中的keyName
        servlet.addInitParameter("kaptcha.session.key","checkCode");
        servlet.addInitParameter("kaptcha.obscurificator.impl","com.google.code.kaptcha.impl.ShadowGimpy");
        return servlet;
    }

}
